package com.anzia.study_7.slice;

import com.anzia.study_7.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {

	private PageSlider pageSlider;
	private RadioContainer radioContainer;
	ArrayList<Component> pages;

	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		super.setUIContent(ResourceTable.Layout_ability_main);

		// 初始化组件
		radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radio_container);
		// 设置第一个按钮为被选中的
		((RadioButton)radioContainer.getComponentAt(0)).setChecked(true);

		pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager_slider);
		// 获取预转入pageSlider容器的子页面
		LayoutScatter scatter = LayoutScatter.getInstance(getContext());
		DependentLayout page0 = (DependentLayout) scatter.parse(ResourceTable.Layout_layout_page_0, null, false);
		DependentLayout page1 = (DependentLayout) scatter.parse(ResourceTable.Layout_layout_page_1, null, false);
		DependentLayout page2 = (DependentLayout) scatter.parse(ResourceTable.Layout_layout_page_2, null, false);

		// 三张页面封装到一个集合
		pages = new ArrayList<>();
		pages.add(page0);
		pages.add(page1);
		pages.add(page2);

		// 现在就是去装，pageSlider的数据适配器
		pageSlider.setProvider(new PageSliderProvider() {
			@Override
			public int getCount() {
				return pages.size();
			}

			// 真正实现装的方法
			@Override
			public Object createPageInContainer(ComponentContainer componentContainer, int i) {
				componentContainer.addComponent(pages.get(i));
				return pages.get(i);
			}

			// 销毁
			@Override
			public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
				componentContainer.removeComponent(pages.get(i));
			}

			// 页面和对象匹不匹配，决定页面是否显示出来
			@Override
			public boolean isPageMatchToObject(Component component, Object o) {
				return component == o;
			}
		});

		// PageSlider的监听器，实现滑动页面切换下面的radioButton
		pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
			// 对应滑动
			@Override
			public void onPageSliding(int i, float v, int i1) {
				// i：页面的索引，0，1，2……
				// v：表示页面的偏移量（0, 1]
				// i1：页面的偏移像素值

			}

			// 对应点击
			@Override
			public void onPageSlideStateChanged(int i) {

			}

			@Override
			public void onPageChosen(int i) {
				((RadioButton) radioContainer.getComponentAt(i)).setChecked(true);
			}
		});

		// radioContainer的监听器：实现点击radioButton切换页面
		radioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
			@Override
			public void onCheckedChanged(RadioContainer radioContainer, int i) {
				pageSlider.setCurrentPage(i);
				System.out.println("触发");
				int widthPx = getWindowWidthPx(MainAbilitySlice.this);
				int heightPx = getWindowHeightPx(MainAbilitySlice.this);
				System.out.println("宽：" + widthPx + "；" + "高：" + heightPx);
			}
		});

	}

	@Override
	public void onActive() {
		super.onActive();
	}

	@Override
	public void onForeground(Intent intent) {
		super.onForeground(intent);
	}

	public static int getWindowWidthPx(Context context) {
		return context.getResourceManager().getDeviceCapability().width * context.getResourceManager().getDeviceCapability().screenDensity / 160;
	}

	public static int getWindowHeightPx(Context context) {
		return context.getResourceManager().getDeviceCapability().height * context.getResourceManager().getDeviceCapability().screenDensity / 160;
	}
}
